package cn.edu.pzhu.javaweb.util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * 数据库工具类
 */
public class DBUtil{
    
    private static String url;
    private static String username;
    private static String password;
    private static String driver;
    
    /**
     * 静态代码块：加载配置和注册驱动
     */
    static {
        try {
            // 加载配置文件
            Properties prop = new Properties();
            InputStream is = DBUtil.class.getClassLoader().getResourceAsStream("db.properties");
            if (is == null) {
                throw new RuntimeException("找不到数据库配置文件 db.properties");
            }
            prop.load(is);
            
            // 获取配置参数
            url = prop.getProperty("url");
            username = prop.getProperty("username");
            password = prop.getProperty("password");
            driver = prop.getProperty("driver");
            
            // 检查参数是否完整
            if (url == null || username == null || password == null || driver == null) {
                throw new RuntimeException("数据库配置参数不完整，请检查db.properties文件");
            }
            
            // 注册驱动
            Class.forName(driver);
            System.out.println("数据库驱动注册成功！");
            
        } catch (IOException e) {
            System.err.println("加载配置文件失败！");
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            System.err.println("注册驱动失败！");
            e.printStackTrace();
            throw new RuntimeException("注册数据库驱动失败", e);
        }
    }
    
    /**
     * 获取数据库连接
     */
    public static Connection getConnection() {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(url, username, password);
            System.out.println("数据库连接成功！");
        } catch (SQLException e) {
            System.err.println("获取数据库连接失败！");
            e.printStackTrace();
        }
        return conn;
    }
    
    /**
     * 释放资源 - 多种重载方法
     */
    
    // 释放连接和语句
    public static void closeResources(Statement stmt, Connection conn) {
        closeStatement(stmt);
        closeConnection(conn);
    }
    
    // 释放连接、语句和结果集
    public static void closeResources(ResultSet rs, Statement stmt, Connection conn) {
        closeResultSet(rs);
        closeStatement(stmt);
        closeConnection(conn);
    }
    
    // 释放预编译语句和连接
    public static void closeResources(PreparedStatement pstmt, Connection conn) {
        closePreparedStatement(pstmt);
        closeConnection(conn);
    }
    
    // 释放预编译语句、结果集和连接
    public static void closeResources(ResultSet rs, PreparedStatement pstmt, Connection conn) {
        closeResultSet(rs);
        closePreparedStatement(pstmt);
        closeConnection(conn);
    }
    
    // 私有方法：关闭各个资源
    private static void closeResultSet(ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    
    public static void closeStatement(Statement stmt) {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    
    public static void closePreparedStatement(PreparedStatement pstmt) {
        if (pstmt != null) {
            try {
                pstmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    
    public static void closeConnection(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
                System.out.println("数据库连接已关闭！");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    
    /**
     * 测试方法
     */
    public static void main(String[] args) {
        Connection conn = getConnection();
        if (conn != null) {
            System.out.println("数据库连接测试成功！");
            closeConnection(conn);
        }
    }
}